#!/bin/bash

set -e
set -o pipefail

unset elf
unset cfg
unset jlink
unset openocd
unset gdb
while [[ "$1" != "" ]]
do
    case "$1"
    in
    --elf)                 elf="$2";     shift 2;;
    --openocd)             openocd="$2"; shift 2;;
    --jlink)               jlink="$2";   shift 2;;
    --openocd-config)      cfg="$2";     shift 2;;
    --gdb)                 gdb="$2";     shift 2;;
    *) echo "$0: Unknown argument $1";   exit 1;;
    esac
done

if [ "$elf" == "" ]
then
    echo "$0: --elf is required" >&2
    exit 1
fi

export GDB_PORT=3333

if [ "$jlink" != "" ]
then

$jlink -device RISC-V -port $GDB_PORT &
extra_gdb=("-ex" "monitor allowsimulation 0")

else

$openocd -f $cfg &

fi

$gdb $elf -ex "set remotetimeout 240" -ex "target extended-remote localhost:${GDB_PORT}" "${extra_gdb[@]}"

kill %1
